<?php

namespace App\Kuafu\Banner\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\CouponPackage\Enum\CouponPackageEnum;

class BannerCommonShopMappingsValidate extends BaseValidate
{
    /**
     * 获取规则信息
     *
     * @param array $fields
     * @return string[]
     */
    public static function getRules(array $fields = []): array
    {
        return self::getActuallyRules([
            'shopInfo' => [
                'exclude_unless:shopType,' . CouponPackageEnum::SHOP_TYPE_SELECTION,
                'required',
                'array',
                function ($attribute, $value, $fail) {
                    if (empty($value) || empty(array_filter($value))) {
                        $fail('The ' . $attribute . ' is invalid.');
                    }
                }
            ],
        ], $fields);
    }


    /**
     * @param array $requestParams
     * @return array
     */
    public static function insertValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams,
            ['shopInfo'],
            [],
            function ($params) use ($requestParams) {
                if ($requestParams['shopType'] != CouponPackageEnum::SHOP_TYPE_SELECTION) {
                    $params['shopInfo'] = [];
                }
                return $params;
            }
        );
    }


}
